package com.chief.hadoop.mr.outputFormat.chain;

import com.chief.hadoop.mr.outputFormat.FlowBean;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.*;

public class FlowMapper extends Mapper<Text, FlowBean, Text, FlowBean> {


    Map<Text, FlowBean> map = new HashMap<>();

    @Override
    protected void map(Text key, FlowBean value, Context context) throws IOException, InterruptedException {
        map.put(key, value);
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        List<Map.Entry<Text, FlowBean>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, Comparator.comparing(Map.Entry::getValue));

        for (Map.Entry<Text, FlowBean> entry : list) {
            context.write(entry.getKey(), entry.getValue());
        }
        super.cleanup(context);
    }
}
